home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / OOFILE / Buildable, limited OOFILE / OOFILE headers / oof1.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-27  |  12.0 KB  |  423 lines  |  [TEXT/CWIE]

  1. #ifndef H_OOF1
  2. #define H_OOF1
  3.  
  4. // COPYRIGHT 1994 A.D. Software, All rights reserved
  5.  
  6. // public layer of OOFILE database
  7.  
  8. // NOTE inline definitions included at end of this header file
  9.  
  10. // see misc.txt for code conventions
  11. #include "oof0.hpp"
  12. #ifdef _Macintosh
  13. #include <Types.h>
  14. #endif
  15.  
  16.  
  17. enum OOF_IndexOptions { kNotIndexed=0, kIndexed=1, kIndexNoDups=2, kIndexCaseSensitive=4,
  18.                                                 kIndexCompressLeading=8, kIndexCompressPadding=16,
  19.                                                 kIndexCompress = kIndexCompressLeading + kIndexCompressPadding,
  20.                                                 kIndexCompressNoDups = kIndexCompress + kIndexNoDups,
  21.                                                 kIndexCompressLeadingNoDups = kIndexCompressLeading + kIndexNoDups,
  22.                                                 kIndexCompressPaddingNoDups = kIndexCompressPadding + kIndexNoDups
  23.                                             };
  24. ostream& operator<<(ostream&, OOF_IndexOptions);
  25.  
  26.  
  27. typedef  unsigned short fieldNumT;  // never more than 64k fields in a record!
  28.  
  29. // utility functions
  30. void validateDatabaseState();
  31. bool skipTillDigit(istream&);
  32.  
  33.  
  34. class OOF_String {
  35. public:
  36. // constructors
  37.     OOF_String(const OOF_String&);
  38.     OOF_String(const OOF_String&, const char *);
  39.     OOF_String(const OOF_String&, const char, const char *);
  40.     OOF_String(const OOF_String&, const char *, const OOF_String&);
  41.     OOF_String(const char *chars=0);  // default
  42.     ~OOF_String();
  43.  
  44. // other protocol
  45.     OOF_String& operator=(const OOF_String&);
  46.     OOF_String& operator=(const char*);
  47.     operator char *() const; 
  48.     char *adopt();
  49.     bool isEmpty() const;
  50.     
  51. protected:
  52. // data storage
  53.     char *mBody;    // owned
  54.     unsigned int mLen;
  55.     
  56. public:
  57.     static char sEmptyStr;    
  58. };
  59. ostream& operator<<(ostream&, const OOF_String&);
  60.  
  61.  
  62. #ifdef _Macintosh
  63. class OOF_MacString : public OOF_String {
  64. public:
  65.     OOF_MacString(const Str63);
  66. };
  67. #endif
  68.  
  69.  
  70.  
  71. class dbQueryClause;
  72. class dbRelHalf;
  73. class dbRelChain;
  74. class dbRelRefBase;
  75. class OOF_relChainList;
  76. class dbRelationship;
  77. class dbField;
  78. class OOF_tableBackend;
  79. class OOF_Context;
  80. class dbConnect;
  81.  
  82. class dbTable : public dbClass {
  83. public:
  84.     dbTable(const char *);  // MUST name tables
  85.     dbTable(const dbTable&);
  86.     virtual ~dbTable();
  87. private:
  88.     void operator=(const dbTable&) {assert(0);};
  89. public:
  90.     void setName(const char*);
  91.     virtual dbTable* cloneWithoutSelection();
  92. //    operator dbTable*();
  93.     
  94.     void attachPartRelation(dbRelHalf*);
  95. // used to setup relations
  96.     void joinField(dbField&);
  97.     void pathNamedAs(const char*);
  98.     dbRelChain* buildRelChain(dbRelChain*);
  99.     void assignRelChainToAllFields(dbRelChain*);
  100.  
  101. // other protocol
  102.     bool loadRelatedContextJoiningFromTo(const dbField*, const dbField*);
  103.     enum saveOptions{requireExplicit, autoOnContextChange, requireExplicitAndBuffer};
  104.     void setSaveOption(const saveOptions);
  105.     dbField *field(fieldNumT);
  106.     dbField *field(const char *);
  107.     OOF_String tableName() const;
  108.     void setOIDfield(const dbField&) const;
  109.     bool pointsToBackend(const OOF_tableBackend*) const;
  110.     virtual bool canSaveRecord();  // expect user override in many cases
  111.  
  112.     void attachfield(dbField*);
  113.     void buildSchema();
  114.     void createTableInConnection(const dbConnect*);
  115.     void openTableInConnection(const dbConnect*);
  116.     unsigned int numIndexes() const;
  117.     unsigned int numFiles() const;
  118.     long sequenceNumber() const;
  119.         
  120. // current record context management
  121.     bool contextChangedFrom(const OOF_Context*) const;
  122.     void updateContext(OOF_Context*) const;
  123.     OOF_Context* createContext() const;
  124.     void returnToContext(OOF_Context*);
  125.     
  126. // recordwise access
  127.     void start();
  128.     void next();
  129.     void first();
  130.     void prev();
  131.   void last();
  132.  
  133.     bool more() const;
  134.     bool atFirst() const;
  135.     bool atLast() const;
  136.  
  137.     unsigned long count() const;
  138.     unsigned long countAll() const;
  139.     
  140.     void sortBy(const dbField&);
  141.     void sortBy(const dbField*);
  142.     void unloadRecord();
  143.     bool isRecordLoaded() const;
  144.     bool isDirty() const;
  145.     unsigned long recordNumber() const;
  146.  
  147. // public search functions
  148.     bool gotoRelativeRecord(unsigned long);
  149.     bool gotoRecord(unsigned long);
  150.     bool searchDefaultIndex(const char*);
  151.     void selectAll();
  152.     void selectNone();
  153.     bool search(const dbQueryClause& query);
  154.     bool searchSelection(const dbQueryClause& query);
  155.     
  156. // selection functions
  157.     void difference_with(const dbTable&);
  158.     void operator -=(const dbTable&);
  159.     void operator %=(const dbTable&);
  160.     
  161.     void intersection_with(const dbTable&); 
  162.     void operator &=(const dbTable&);
  163.     
  164.     void union_with(const dbTable&);
  165.     void operator +=(const dbTable&);
  166.     void operator |=(const dbTable&);
  167.     
  168.     void invert();
  169.     void operator~();
  170.     void operator!();
  171.     
  172. // NOT YET IMPLEMENTED    void reduceSelectionToCurrentRecord();  
  173.  
  174. // data access
  175.     void newRecord();
  176.     void saveRecord();
  177.     void deleteRecord();
  178.     void deleteSelection();
  179.     void deleteAll();
  180.     
  181.     void extract(ostream&);
  182.     unsigned long insert(istream&);
  183.  
  184. // reflective operators
  185.     virtual void describe(ostream&);
  186.  
  187.     virtual void generateTestData(int numRecords, const char* testBuf, unsigned long testBufLen);
  188.  
  189. private:
  190.     void AddHalfRelation(dbRelRefBase& linkfieldPtr);
  191. //    void SetReferenceHalf(dbRelHalf*);
  192.     void CompleteField(dbField*);
  193.     void ContextChange();
  194.  
  195. // data storage
  196. protected:
  197.     OOF_tableBackend *mBackend;      // owned  (the bit that does all the work!!)
  198.     OOF_Dictionary    mFields;
  199.     OOF_String mTableName;
  200.     fieldNumT mFieldCount;
  201.     fieldNumT mSortByFieldNum;
  202.     saveOptions mSaveOption;
  203.  
  204. private:
  205.     OOF_Dictionary mPartRelations;
  206.     OOF_relChainList* mRelChains;    // owned
  207.  
  208.     static dbTable *sCurrentlyConstructing;
  209.     static dbRelHalf * sSavedRelationHalf;
  210. protected:
  211.     static bool sCloningWithoutSelection;
  212.     
  213.     friend class dbField;
  214.     friend class dbConnect;
  215.     friend class dbRelHalf;
  216. };
  217. ostream& operator<<(ostream& os, dbTable& tbl);
  218. ostream& operator<<(ostream& os, dbTable* tbl);
  219. istream& operator>>(istream& is, dbTable* tbl);
  220. istream& operator>>(istream& is, dbTable& tbl);
  221.  
  222.  
  223. class stPreserveOOFILEcontext {
  224. public:
  225.     stPreserveOOFILEcontext(dbTable*);
  226.     stPreserveOOFILEcontext(dbTable&);
  227.     ~stPreserveOOFILEcontext();
  228.     
  229. private:
  230.     OOF_Context* mContext;
  231.     dbTable* mTable;
  232. };
  233.  
  234.  
  235. class dbConnect : public dbClass {
  236. public:
  237.     dbConnect();
  238.     virtual ~dbConnect();
  239.     
  240.     virtual void newConnection(const OOF_String& connectionName)=0;
  241.     virtual void openConnection(const OOF_String& connectionName)=0;
  242.     
  243.  
  244.     void attachTable(dbTable *);
  245.     void attachRelation(dbRelationship *);
  246.     dbTable *table(unsigned int);
  247.     dbTable *table(const char*);
  248.  
  249. // locking
  250.     virtual void exitLocking()=0;
  251.     virtual void enterWriteLocking()=0;
  252.     virtual void enterReadLocking()=0;
  253.  
  254. // utilities
  255.     static bool fileExists(const char * fName);
  256.     static bool underConstruction();
  257.     virtual void describe(ostream&);
  258.     void dumpData(ostream&);
  259.     void generateTestData(const char* testFileName, unsigned long maxRecs=0);
  260.     static void raise(ostream&);
  261.     static void raise(const char*);
  262.  
  263. private:
  264.     virtual OOF_tableBackend *MakeTableBackend(const OOF_Dictionary& tablesfields, const OOF_String& tableName)=0;
  265.  
  266. // data storage
  267. protected:
  268.     static dbConnect *sCurrentlyConstructing;
  269.     OOF_Dictionary    mTables, mRelations;
  270.     OOF_String mConnectionName;
  271.  
  272.  
  273.     friend dbTable::dbTable(const char*);
  274.     friend class dbRelationship;
  275. };
  276.  
  277.  
  278. class dbBLOB;
  279.  
  280. class OOF_tableBackend {
  281. // uses lots of public methods but is only ever accessed by a dbTable or dbField subclass
  282. // so no problems
  283. protected:
  284.     OOF_tableBackend(const OOF_Dictionary& tablesfields, const OOF_String& tableName):
  285.                                                         mOIDfield(0),
  286.                                                         mFields(tablesfields),
  287.                                                         mTableName(tableName)
  288.                                                         {};  
  289.     OOF_tableBackend(const OOF_tableBackend&, const OOF_Dictionary& tablesFields);
  290.     
  291.  public:
  292.     virtual void buildSchema()=0;
  293.     virtual void createTableInConnection(const dbConnect*)=0;
  294.     virtual void openTableInConnection(const dbConnect*)=0;
  295.     void setOIDfield(const dbField &oidfield);
  296.     virtual OOF_tableBackend* clone(bool selectionNotCopied, const OOF_Dictionary&) const=0;
  297.  
  298. // recordwise access
  299.     virtual void start()=0;
  300.     virtual void next()=0;
  301.     virtual void prev()=0;
  302.     virtual bool more() const=0;
  303.     virtual bool atFirst() const=0;
  304.     virtual bool atLast() const=0;
  305.     virtual unsigned long count() const=0;
  306.     virtual unsigned long countAll() const=0;
  307.     virtual void sortBy(fieldNumT)=0;
  308.  
  309. // data access
  310.     virtual void newRecord()=0;
  311.     virtual void deleteRecord()=0;
  312.     virtual void saveRecord()=0;
  313.     virtual void unloadRecord()=0;
  314.     virtual bool isRecordLoaded() const=0;
  315.     virtual void *getFieldWriteDest(fieldNumT)=0;
  316.     virtual void *getFieldReadFrom(fieldNumT)=0;
  317.     virtual void setBlobLength(fieldNumT, unsigned long len)=0;
  318.     virtual void loadBlob(dbBLOB*)=0;
  319.     bool isDirty() const;
  320.    void markDirty();
  321.  
  322. // reflective operators
  323.     virtual unsigned int numIndexes() const=0;
  324.     virtual unsigned int numFiles() const=0;
  325.     virtual unsigned long fieldDataLen(const dbField*) const=0;
  326.     virtual unsigned long blobPointerReferenceSize() const=0;
  327.     virtual long sequenceNumber() const=0;
  328.  
  329. // public search functions
  330.     virtual bool gotoRecord(unsigned long)=0;
  331.     virtual bool gotoRelativeRecord(unsigned long relativeNum)=0;
  332.     virtual bool searchEqual(const dbField*, const char*, bool matchEntireKey=true)=0;
  333.     virtual bool searchEqual(const dbField*, const void*)=0;
  334.     virtual void selectAll()=0;
  335.     virtual void selectNone()=0;
  336.     virtual bool search(const dbQueryClause*)=0;
  337.     virtual bool searchSelection(const dbQueryClause*)=0;
  338.     virtual void difference_with(const OOF_tableBackend*)=0;
  339.     virtual void intersection_with(const OOF_tableBackend*)=0;  
  340.     virtual void invert()=0;  
  341.     virtual void union_with(const OOF_tableBackend*)=0;  
  342.     virtual bool loadRelatedContextJoiningFromTo(const dbField*, const dbField*)=0;
  343.  
  344. // current record context management
  345.     virtual bool contextChangedFrom(const OOF_Context*) const=0;
  346.     virtual void updateContext(OOF_Context*) const=0;
  347.     virtual OOF_Context* createContext() const=0;
  348.     virtual unsigned long recordNumber() const=0;
  349.     virtual void returnToContext(OOF_Context*)=0;
  350.  
  351.  protected:
  352.     virtual void CacheDirtyBuffer()=0;
  353.     
  354.  // data storage
  355.     dbField *mOIDfield;
  356.     OOF_Dictionary     mFields;        // copy of dbTable handle object, one representation
  357.     OOF_String mTableName;            // copy of dbtable object
  358.     bool mDirty;
  359.  
  360.     friend class dbTable;
  361. };
  362.  
  363.  
  364. class OOF_mixRelChainEndPoint {
  365. public:
  366.     OOF_mixRelChainEndPoint() : mRelationChain(0) {};
  367.     virtual ~OOF_mixRelChainEndPoint();
  368.     
  369.     static void appendRelationLink(dbRelHalf*);
  370.     static bool hasTransitoryLinks();
  371.     
  372. protected:
  373.     bool ConsumePossibleRelationChain();
  374. // data storage
  375.     dbRelChain* mRelationChain;    // owned
  376.     
  377.     static dbRelChain* sTransitoryRelChainLinks;
  378. };
  379.  
  380.  
  381. // define macros for table construction
  382. #define REF_TABLE(tb) \
  383. class tb; \
  384. class tb ## Ref : public dbRelRef \
  385. { public: tb* operator->() { LogTransition(); return (tb*) RefersToTable();}; }; 
  386.  
  387.  
  388. #define SET_TABLE(tb) \
  389. class tb; \
  390. class tb ## Set : public dbRelSet \
  391. { public: tb* operator->() { LogTransition(); return (tb*) RefersToTable();}; }; 
  392.  
  393.  
  394. #define CLASS_TABLE(tb) \
  395. class tb : public dbTable { \
  396. public: \
  397. tb&    operator[](const dbQueryClause& rhs) { search(rhs); return *this; }; \
  398. tb&    operator[](unsigned long index) {    gotoRelativeRecord(index); return *this;}; \
  399. tb&    operator[](const char *name) {    searchDefaultIndex(name); return *this;}; \
  400. tb&    operator[](long index) {    assert(index>=0); gotoRelativeRecord(index); return *this;}; \
  401. virtual dbTable* cloneWithoutSelection() { sCloningWithoutSelection = true; return new tb(*this); }; \
  402. tb& operator-(const tb& rhs) { dbTable::difference_with(rhs); return *this; }; \
  403. tb& difference_with(const tb& rhs) { dbTable::difference_with(rhs); return *this; }; \
  404. tb& operator&(const tb& rhs) { dbTable::intersection_with(rhs); return *this; }; \
  405. tb& intersection_with(const tb& rhs) { dbTable::intersection_with(rhs); return *this; }; \
  406. tb& operator|(const tb& rhs) { dbTable::union_with(rhs); return *this; }; \
  407. tb& operator+(const tb& rhs) { dbTable::union_with(rhs); return *this; }; \
  408. tb& union_with(const tb& rhs) { dbTable::union_with(rhs); return *this; }; \
  409. friend ostream& operator<<(ostream& os, tb& tbl) {tbl.extract(os); return os;}; \
  410. friend ostream& operator<<(ostream& os, tb* tbl) {tbl->extract(os); return os;}; \
  411. friend istream& operator>>(istream& is, tb& tbl) {tbl.insert(is); return is;}; \
  412. friend istream& operator>>(istream& is, tb* tbl) {tbl->insert(is); return is;};
  413.  
  414. //tb* operator&() { assert(0);  return this;}; \
  415. //tb* invert(const dbTablePtr& rhs) { AddHalfRelation((dbTable**) &rhs); return this; }; \
  416.  
  417.  
  418. // include inline definitions
  419. #include "oof1.inl"
  420. #endif
  421.  
  422.  
  423.